﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MG_POS_PC.DAL;
using MG_POS_PC.Model;
using MG_POS_PC.Util;
using System.Runtime.InteropServices;

namespace MG_POS_PC
{
    public partial class LoginForm : BaseForm
    {
        protected int OriginalScreenSizeWidth = Screen.PrimaryScreen.Bounds.Width;
        protected int OriginalScreenSizeHeight = Screen.PrimaryScreen.Bounds.Height;

        public LoginForm()
        {
            InitializeComponent();
            skinEngine1.SkinStream = memoryStreams[themeId];
            this.Load += new System.EventHandler(this.LoginForm_Load);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.LoginForm_FormClosing);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;

            User user = new User();
            
            //user.us_login_id = int.Parse(textBox1.Text.Trim());
            //user.us_pwd = textBox2.Text.Trim();
            //user.us_active = "Y";
            //user.us_color = 2;
            //user.us_manager = "N";

            //UserDao userDao = new UserDao();
            //userDao.Add(user);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            iniUtil.loadIni();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = OriginalScreenSizeWidth.ToString();
            textBox2.Text = OriginalScreenSizeHeight.ToString();

            SetResolution.ChangeRes(1366, 768);
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SetResolution.ChangeRes(OriginalScreenSizeWidth, OriginalScreenSizeHeight);
        }

    }

    public class SetResolution
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct DEVMODE
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmDeviceName;
            public short dmSpecVersion;
            public short dmDriverVersion;
            public short dmSize;
            public short dmDriverExtra;
            public int dmFields;
            public short dmOrientation;
            public short dmPaperSize;
            public short dmPaperLength;
            public short dmPaperWidth;
            public short dmScale;
            public short dmCopies;
            public short dmDefaultSource;
            public short dmPrintQuality;
            public short dmColor;
            public short dmDuplex;
            public short dmYResolution;
            public short dmTTOption;
            public short dmCollate;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmFormName;
            public short dmLogPixels;
            public int dmBitsPerPel;
            public int dmPelsWidth;
            public int dmPelsHeight;
            public int dmDisplayFlags;
            public int dmDisplayFrequency;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int ChangeDisplaySettings([In]ref DEVMODE lpDevMode, int dwFlags);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);

        public static void ChangeRes(int width, int height)
        {
            DEVMODE devM = new DEVMODE();
            devM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            bool mybool;
            mybool = EnumDisplaySettings(null, 0, ref devM);
            devM.dmPelsHeight = height;
            devM.dmPelsWidth = width;
            devM.dmDisplayFrequency = 75;
            devM.dmBitsPerPel = 32;
            long result = ChangeDisplaySettings(ref devM, 0);
        }
    }
}
